package com.atguigu;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName MyThreadPoolDemo
 * @Author yy
 * @create 2022/9/8 20:36
 */
public class MyThreadPoolDemo {
    public static void main(String[] args) {
//        ExecutorService pool = Executors.newFixedThreadPool(5);//一池多线程
//        ExecutorService pool = Executors.newSingleThreadExecutor();//一池一线程
        ExecutorService pool = Executors.newCachedThreadPool();//一池自定义线程数


        try {
            for (int i = 1; i <= 20; i++) {
                int finalI = i;
                pool.execute(()->{
                    System.out.println(Thread.currentThread().getName() + "工作人员\t 服务第：" + finalI + "顾客办理业务");

                    /*try {
                        TimeUnit.SECONDS.sleep(2);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }*/
                    //等待的时间超过执行业务的时间,线程池自定义线程立即创建新的线程来执行业务
                });
            }
        } finally {
            pool.shutdown();
        }

    }
}
